﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MvCamCtrl.NET;
using System.Runtime.InteropServices;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;


namespace AssemblyCYG
{
    public class HKCameral
    {
        public struct CAMERA//定义相机结构体
        {
            public MyCamera Cam_Info;
            public UInt32 m_nBufSizeForSaveImage;
            public byte[] m_pBufForSaveImage;         // 用于保存图像的缓存
        }
        MyCamera.cbOutputdelegate cbImage;    //委托触发采集拍照
        MyCamera.MV_CC_DEVICE_INFO_LIST m_pDeviceList;
        int m_nDevNum;        // ch:在线设备数量 | en:Online Device Number
        CAMERA[] m_pMyCamera = new CAMERA[4];   //初始化四个相机       
        public delegate void CaptureImageHandler(int cameralIndex, Bitmap image);
        public event CaptureImageHandler captureImage;
        public void HKImageCallBack()
        {
            cbImage = new MyCamera.cbOutputdelegate(ImageCallBack1); //相机回调函数
        }
        public bool CameralDeviceListAcq(out List<string> deviceInfoList) //初始化设备
        {
            bool nflag = true;
            int nRet;
            deviceInfoList = new List<string>();
            System.GC.Collect();
            m_pDeviceList = new MyCamera.MV_CC_DEVICE_INFO_LIST();
            m_nDevNum = 0;
            nRet = MyCamera.MV_CC_EnumDevices_NET(MyCamera.MV_GIGE_DEVICE | MyCamera.MV_USB_DEVICE, ref m_pDeviceList);  //查找接口为USB或gige接口的相机
            if (0 != nRet)
            {
                //MessageBox.Show("Enumerate devices fail!");
                nflag = false;
            }
            m_nDevNum = (int)m_pDeviceList.nDeviceNum;   //查找到的相机的数量            
            for (int i = 0; i < m_pDeviceList.nDeviceNum; i++)    //遍历相机，获取每个相机的信息
            {
                MyCamera.MV_CC_DEVICE_INFO device = (MyCamera.MV_CC_DEVICE_INFO)Marshal.PtrToStructure(m_pDeviceList.pDeviceInfo[i], typeof(MyCamera.MV_CC_DEVICE_INFO));


                if (device.nTLayerType == MyCamera.MV_GIGE_DEVICE)       //Gige接口
                {
                    IntPtr buffer = Marshal.UnsafeAddrOfPinnedArrayElement(device.SpecialInfo.stGigEInfo, 0);
                    MyCamera.MV_GIGE_DEVICE_INFO gigeInfo = (MyCamera.MV_GIGE_DEVICE_INFO)Marshal.PtrToStructure(buffer, typeof(MyCamera.MV_GIGE_DEVICE_INFO));
                    if (gigeInfo.chUserDefinedName != "")
                    {
                        deviceInfoList.Add("GigE: " + gigeInfo.chUserDefinedName + " (" + gigeInfo.chSerialNumber + ")");
                        //cbDeviceList.Items.Add("GigE: " + gigeInfo.chUserDefinedName + " (" + gigeInfo.chSerialNumber + ")");
                    }
                    else
                    {
                        deviceInfoList.Add("GigE: " + gigeInfo.chManufacturerName + " " + gigeInfo.chModelName + " (" + gigeInfo.chSerialNumber + ")");
                    }
                }
                else if (device.nTLayerType == MyCamera.MV_USB_DEVICE)    //usb接口
                {
                    IntPtr buffer = Marshal.UnsafeAddrOfPinnedArrayElement(device.SpecialInfo.stUsb3VInfo, 0);
                    MyCamera.MV_USB3_DEVICE_INFO usbInfo = (MyCamera.MV_USB3_DEVICE_INFO)Marshal.PtrToStructure(buffer, typeof(MyCamera.MV_USB3_DEVICE_INFO));
                    if (usbInfo.chUserDefinedName != "")
                    {
                        deviceInfoList.Add("USB: " + usbInfo.chUserDefinedName + " (" + usbInfo.chSerialNumber + ")");
                    }
                    else
                    {
                        deviceInfoList.Add("USB: " + usbInfo.chManufacturerName + " " + usbInfo.chModelName + " (" + usbInfo.chSerialNumber + ")");
                    }
                }                                                                                           
            }
            return nflag;
        }
        public bool OpenDevice(int index)
        {
            bool nflag = true;
            m_pMyCamera[index].m_nBufSizeForSaveImage = 3072 * 2048 * 3 * 3 + 2048;
            m_pMyCamera[index].m_pBufForSaveImage = new byte[3072 * 2048 * 3 * 3 + 2048];
            int nRet = -1;
            m_pMyCamera[index] = new CAMERA();
            //ch:获取选择的设备信息 | en:Get Selected Device Information
            MyCamera.MV_CC_DEVICE_INFO device =
              (MyCamera.MV_CC_DEVICE_INFO)Marshal.PtrToStructure(m_pDeviceList.pDeviceInfo[index],
                                              typeof(MyCamera.MV_CC_DEVICE_INFO));
            //ch:打开设备 | en:Open Device
            if (null == m_pMyCamera[index].Cam_Info)
            {
                m_pMyCamera[index].Cam_Info = new MyCamera();
                if (null == m_pMyCamera)
                {
                    nflag = false;
                }
            }
            //创建设备
            nRet = m_pMyCamera[index].Cam_Info.MV_CC_CreateDevice_NET(ref device);  
            if (MyCamera.MV_OK != nRet)
            {
                nflag = false;
            }
            nRet = m_pMyCamera[index].Cam_Info.MV_CC_OpenDevice_NET();   //打开设备
            if (MyCamera.MV_OK != nRet)
            {
                nflag = false;
            }
            //m_pMyCamera[index].Cam_Info.MV_CC_SetEnumValue_NET("TriggerMode", 1);     //设置为触发模式，0为连续采集，1为触发采集
            // m_pMyCamera[i].Cam_Info.MV_CC_SetEnumValue_NET("TriggerSource", 7);      //设置为软触发
            //m_pMyCamera[index].Cam_Info.MV_CC_SetEnumValue_NET("TriggerSource", 0);   //切换为硬触发
            m_pMyCamera[index].Cam_Info.MV_CC_RegisterImageCallBack_NET(cbImage, (IntPtr)index);
            return nflag;
        }
        void ImageCallBack1(IntPtr pData, ref MyCamera.MV_FRAME_OUT_INFO pFrameInfo, IntPtr pUser)
        {
            int nIndex = (int)pUser;     //通过pUser判断相机索引
            if ((3 * pFrameInfo.nFrameLen + 2048) > m_pMyCamera[nIndex].m_nBufSizeForSaveImage)
            {
                m_pMyCamera[nIndex].m_nBufSizeForSaveImage = 3 * pFrameInfo.nFrameLen + 2048;
                m_pMyCamera[nIndex].m_pBufForSaveImage = new byte[m_pMyCamera[nIndex].m_nBufSizeForSaveImage];
            }
            IntPtr pImage = Marshal.UnsafeAddrOfPinnedArrayElement(m_pMyCamera[nIndex].m_pBufForSaveImage, 0);
            MyCamera.MV_SAVE_IMAGE_PARAM_EX stSaveParam = new MyCamera.MV_SAVE_IMAGE_PARAM_EX();
            //stSaveParam.enImageType = MyCamera.MV_SAVE_IAMGE_TYPE.MV_Image_Bmp;
            //stSaveParam.enPixelType = pFrameInfo.enPixelType;
            //stSaveParam.pData = pData;
            //stSaveParam.nDataLen = pFrameInfo.nFrameLen;
            //stSaveParam.nHeight = pFrameInfo.nHeight;
            //stSaveParam.nWidth = pFrameInfo.nWidth;
            //stSaveParam.enPixelType = pFrameInfo.enPixelType;
            //stSaveParam.pImageBuffer = pImage;
            //stSaveParam.nBufferSize = m_pMyCamera[nIndex].m_nBufSizeForSaveImage;
            //stSaveParam.nJpgQuality = 80;
            //int nRet = m_pMyCamera[nIndex].Cam_Info.MV_CC_SaveImageEx_NET(ref stSaveParam);


            MyCamera.MV_PIXEL_CONVERT_PARAM stConverPixelParam = new MyCamera.MV_PIXEL_CONVERT_PARAM();
            stConverPixelParam.nWidth = pFrameInfo.nWidth;
            stConverPixelParam.nHeight = pFrameInfo.nHeight;
            stConverPixelParam.pSrcData = pData;
            stConverPixelParam.nSrcDataLen = pFrameInfo.nFrameLen;
            stConverPixelParam.enSrcPixelType = pFrameInfo.enPixelType;
            stConverPixelParam.enDstPixelType = pFrameInfo.enPixelType;
            stConverPixelParam.pDstBuffer = pImage;
            stConverPixelParam.nDstBufferSize = m_pMyCamera[nIndex].m_nBufSizeForSaveImage;
            int nRet = m_pMyCamera[nIndex].Cam_Info.MV_CC_ConvertPixelType_NET(ref stConverPixelParam);


            if (pFrameInfo.enPixelType == MyCamera.MvGvspPixelType.PixelType_Gvsp_Mono8)
            {
                //************************Mono8 转 Bitmap*******************************
                Bitmap bmp = new Bitmap(pFrameInfo.nWidth, pFrameInfo.nHeight, pFrameInfo.nWidth * 1, PixelFormat.Format8bppIndexed, pImage);
                ColorPalette cp = bmp.Palette;
                // init palette
                for (int i = 0; i < 256; i++)
                {
                    cp.Entries[i] = Color.FromArgb(i, i, i);
                }
                // set palette back
                bmp.Palette = cp;
                captureImage(nIndex, bmp); //将图像显示出来
                // bmp.Save("image.bmp", ImageFormat.Bmp);
            }
            else
            {
                //*********************RGB8 转 Bitmap**************************
                for (int i = 0; i < pFrameInfo.nHeight; i++)
                {
                    for (int j = 0; j < pFrameInfo.nWidth; j++)
                    {
                        byte chRed = m_pMyCamera[nIndex].m_pBufForSaveImage[i * pFrameInfo.nWidth * 3 + j * 3];
                        m_pMyCamera[nIndex].m_pBufForSaveImage[i * pFrameInfo.nWidth * 3 + j * 3] = m_pMyCamera[nIndex].m_pBufForSaveImage[i * pFrameInfo.nWidth * 3 + j * 3 + 2];
                        m_pMyCamera[nIndex].m_pBufForSaveImage[i * pFrameInfo.nWidth * 3 + j * 3 + 2] = chRed;
                    }
                }
                Bitmap bmp = new Bitmap(pFrameInfo.nWidth, pFrameInfo.nHeight, pFrameInfo.nWidth * 3, PixelFormat.Format24bppRgb, pImage);
                bmp.Save("image.bmp", ImageFormat.Bmp);
            }

            //Bitmap bmpa = BytesToBitmap(m_pMyCamera[nIndex].m_pBufForSaveImage, 0, (int)stSaveParam.nImageLen);
            //captureImage(nIndex, bmpa); //将图像显示出来
            
        }
        private Bitmap BytesToBitmap(byte[] Bytes, int offset, int count)
        {
            MemoryStream stream = null;
            try
            {
                stream = new MemoryStream(Bytes, offset, count);
                Bitmap bmp = new Bitmap(stream);
                Image image = (Image)bmp;
                Bitmap newBmp = (Bitmap)image.Clone();
                return newBmp;
                //stream.Close();
               // return new Bitmap((Image)new Bitmap(stream));
            }
            catch (ArgumentNullException ex)
            {
                throw ex;
            }
            catch (ArgumentException ex)
            {
                throw ex;
            }
            finally
            {
                stream.Close();
            }
        }
        public void GetCameralParam(int index, out float exposureTime, out float gain, out float gamma)
        {
            exposureTime = 0;
            gain = 0;
            gamma = 0;
            MyCamera.MVCC_FLOATVALUE stParam = new MyCamera.MVCC_FLOATVALUE();
            int nRet = m_pMyCamera[index].Cam_Info.MV_CC_GetFloatValue_NET("ExposureTime", ref stParam);
            if (MyCamera.MV_OK == nRet)
            {
                exposureTime = stParam.fCurValue;
            }

            nRet = m_pMyCamera[index].Cam_Info.MV_CC_GetFloatValue_NET("ExposureTime", ref stParam);
            if (MyCamera.MV_OK == nRet)
            {
                m_pMyCamera[index].Cam_Info.MV_CC_GetFloatValue_NET("Gain", ref stParam);
                gain = stParam.fCurValue;
            }

            nRet = m_pMyCamera[index].Cam_Info.MV_CC_GetFloatValue_NET("ExposureTime", ref stParam);
            if (MyCamera.MV_OK == nRet)
            {
                m_pMyCamera[index].Cam_Info.MV_CC_GetFloatValue_NET("Gamma", ref stParam);
                gamma = stParam.fCurValue;
            }
        }
        public void GetCameralROIPara(int index, out uint rect_X, out uint rect_Y, out uint rect_Width, out uint rect_Height)
        {
            rect_X = 0;
            rect_Y = 0;
            rect_Width = 0;
            rect_Height = 0;
            MyCamera.MVCC_INTVALUE pstValue = new MyCamera.MVCC_INTVALUE();
            m_pMyCamera[index].Cam_Info.MV_CC_GetAOIoffsetX_NET(ref pstValue);
            rect_X = pstValue.nCurValue;
            m_pMyCamera[index].Cam_Info.MV_CC_GetAOIoffsetY_NET(ref pstValue);
            rect_Y = pstValue.nCurValue;

            m_pMyCamera[index].Cam_Info.MV_CC_GetWidth_NET(ref pstValue);
            rect_Width = pstValue.nCurValue;
            m_pMyCamera[index].Cam_Info.MV_CC_GetHeight_NET(ref pstValue);
            rect_Height = pstValue.nCurValue;
        }
        public int SetHKCamExposure(int camIndex, float value)  //设置曝光
        {
            int nflag = -1;
            m_pMyCamera[camIndex].Cam_Info.MV_CC_SetEnumValue_NET("ExposureAuto", 0);
            nflag = m_pMyCamera[camIndex].Cam_Info.MV_CC_SetFloatValue_NET("ExposureTime", value);
            return nflag;
        }
        public int SetHKCamGain(int camIndex, float value)   //设置增益
        {
            int nflag = -1;
            m_pMyCamera[camIndex].Cam_Info.MV_CC_SetEnumValue_NET("GainAuto", 0);
            nflag = m_pMyCamera[camIndex].Cam_Info.MV_CC_SetFloatValue_NET("Gain", value);
            return nflag;
        }
        public int SetHKCamGamma(int camIndex, float value)
        {
            int nflag = -1;
            m_pMyCamera[camIndex].Cam_Info.MV_CC_SetGammaSelector_NET(1);
            nflag = m_pMyCamera[camIndex].Cam_Info.MV_CC_SetGamma_NET(value);
            //m_pMyCamera[camIndex].Cam_Info.MV_CC_SetEnumValue_NET("GammaEnable", 0);
           // nflag = m_pMyCamera[camIndex].Cam_Info.MV_CC_SetFloatValue_NET("Gamma", value);
            return nflag;
        }
        public int StartHKCamGrab(int camIndex)  //开始采集
        {
            int nRet = -1;
            nRet = m_pMyCamera[camIndex].Cam_Info.MV_CC_StartGrabbing_NET();  //开启采集
            return nRet;
        }
        public int StopHKCamGrab(int camIndex)   //停止采集
        {
            int nRet = -1;
            nRet = m_pMyCamera[camIndex].Cam_Info.MV_CC_StopGrabbing_NET();  //停止采集
            return nRet;
        }
        public int SoftTriggerOnce(int camIndex)  //软触发一次
        {
            int nRet = -1;
            nRet = m_pMyCamera[camIndex].Cam_Info.MV_CC_SetCommandValue_NET("TriggerSoftware");
            return nRet;
        }
        public int DestroyHKCam(int camIndex)  //关闭设备
        {
            int nRet = -1;
            nRet = m_pMyCamera[camIndex].Cam_Info.MV_CC_CloseDevice_NET();
            nRet = m_pMyCamera[camIndex].Cam_Info.MV_CC_DestroyDevice_NET();
            return nRet;
        }
        public int SetSoftTrigger(int camIndex) //设置软触发
        {
            int nRet = -1;
            nRet = m_pMyCamera[camIndex].Cam_Info.MV_CC_SetEnumValue_NET("TriggerSource", 7);
            return nRet;
        }
        public int SetHardTrigger(int camIndex, int sourceIndex) //设置硬触发,0 - Line0,1 - Line1, 2 - Line2, 3 - Line3
        {
            int nRet = -1;
            nRet = m_pMyCamera[camIndex].Cam_Info.MV_CC_SetEnumValue_NET("TriggerSource", (uint)sourceIndex);
            return nRet;
        }
        public int SetContinueGrab(int camIndex)  //设置连续采集
        {
            int nRet = -1;
            nRet = m_pMyCamera[camIndex].Cam_Info.MV_CC_SetEnumValue_NET("TriggerMode", 0);
            return nRet;
        }
        public int SetTriggerGrab(int camIndex)  //设置触发采集, 
        {
            int nRet = -1;
            nRet = m_pMyCamera[camIndex].Cam_Info.MV_CC_SetEnumValue_NET("TriggerMode", 1);
            return nRet;
        }
        public void SetImageROI(int camIndex, int rect_X, int rect_Y, int rect_Width, int rect_Height)
        {
            m_pMyCamera[camIndex].Cam_Info.MV_CC_SetIntValue_NET("Width", (uint)rect_Width);
            m_pMyCamera[camIndex].Cam_Info.MV_CC_SetIntValue_NET("Height", (uint)rect_Height);
            m_pMyCamera[camIndex].Cam_Info.MV_CC_SetIntValue_NET("OffsetX", (uint)rect_X);
            m_pMyCamera[camIndex].Cam_Info.MV_CC_SetIntValue_NET("OffsetY", (uint)rect_Y);
        }
        public void RestoreMaxROI(int camIndex)
        {
            MyCamera.MVCC_INTVALUE stWidthValue = new MyCamera.MVCC_INTVALUE();
            m_pMyCamera[camIndex].Cam_Info.MV_CC_GetWidth_NET(ref stWidthValue);
            MyCamera.MVCC_INTVALUE stHeightValue = new MyCamera.MVCC_INTVALUE();
            m_pMyCamera[camIndex].Cam_Info.MV_CC_GetHeight_NET(ref stHeightValue);
            m_pMyCamera[camIndex].Cam_Info.MV_CC_SetIntValue_NET("Width", (uint)stWidthValue.nMax);
            m_pMyCamera[camIndex].Cam_Info.MV_CC_SetIntValue_NET("Height", (uint)stHeightValue.nMax);
            m_pMyCamera[camIndex].Cam_Info.MV_CC_SetIntValue_NET("OffsetX", (uint)0);
            m_pMyCamera[camIndex].Cam_Info.MV_CC_SetIntValue_NET("OffsetY", (uint)0);
        }

        public void GetImage(int camIndex, out Bitmap bmp)
        {
            int nRet;
            // ch:用于从驱动获取图像的缓存 | en:Buffer for getting image from driver
            UInt32 m_nBufSizeForDriver = 3072 * 2048 * 3;
            byte[] m_pBufForDriver = new byte[3072 * 2048 * 3];

            // ch:用于保存图像的缓存 | en:Buffer for saving image
            UInt32 m_nBufSizeForSaveImage = 3072 * 2048 * 3 * 3 + 2048;
            byte[] m_pBufForSaveImage = new byte[3072 * 2048 * 3 * 3 + 2048];
            UInt32 nPayloadSize = 0;
            bmp = null;
            MyCamera.MVCC_INTVALUE stParam = new MyCamera.MVCC_INTVALUE();
            if (m_pMyCamera[camIndex].Cam_Info == null)
                return;
            nRet = m_pMyCamera[camIndex].Cam_Info.MV_CC_GetIntValue_NET("PayloadSize", ref stParam);
            if (MyCamera.MV_OK != nRet)
            {
                //MessageBox.Show("Get PayloadSize failed");
                return;
            }
            nPayloadSize = stParam.nCurValue;
            if (nPayloadSize > m_nBufSizeForDriver)
            {
                m_nBufSizeForDriver = nPayloadSize;
                m_pBufForDriver = new byte[m_nBufSizeForDriver];

                // ch:同时对保存图像的缓存做大小判断处理 | en:Determine the buffer size to save image
                // ch:BMP图片大小：width * height * 3 + 2048(预留BMP头大小) | en:BMP image size: width * height * 3 + 2048 (Reserved for BMP header)
                m_nBufSizeForSaveImage = m_nBufSizeForDriver * 3 + 2048;
                m_pBufForSaveImage = new byte[m_nBufSizeForSaveImage];
            }

            //if (m_BufForDriver != IntPtr.Zero)
            //{
            //    Marshal.Release(m_BufForDriver);
            //}
            //m_nBufSizeForDriver = nPayloadSize;
            //m_BufForDriver = Marshal.AllocHGlobal((Int32)m_nBufSizeForDriver);


            IntPtr pData = Marshal.AllocHGlobal((Int32)m_nBufSizeForDriver);
            //IntPtr pData = Marshal.UnsafeAddrOfPinnedArrayElement(m_pBufForDriver, 0);
            MyCamera.MV_FRAME_OUT_INFO_EX stFrameInfo = new MyCamera.MV_FRAME_OUT_INFO_EX();
            // ch:超时获取一帧，超时时间为1秒 | en:Get one frame timeout, timeout is 1 sec
            nRet = m_pMyCamera[camIndex].Cam_Info.MV_CC_GetOneFrameTimeout_NET(pData, m_nBufSizeForDriver, ref stFrameInfo, 1000);
            if (MyCamera.MV_OK != nRet)
            {
                //MessageBox.Show("No Data!");
               // return;
            }

            MyCamera.MvGvspPixelType enDstPixelType;
            if (IsMonoData(stFrameInfo.enPixelType))
            {
                enDstPixelType = MyCamera.MvGvspPixelType.PixelType_Gvsp_Mono8;
            }
            else if (IsColorData(stFrameInfo.enPixelType))
            {
                enDstPixelType = MyCamera.MvGvspPixelType.PixelType_Gvsp_RGB8_Packed;
            }
            else
            {
                //MessageBox.Show("No such pixel type!");
                return;
            }

            IntPtr pImage = Marshal.UnsafeAddrOfPinnedArrayElement(m_pBufForSaveImage, 0);
            //MyCamera.MV_SAVE_IMAGE_PARAM_EX stSaveParam = new MyCamera.MV_SAVE_IMAGE_PARAM_EX();
            MyCamera.MV_PIXEL_CONVERT_PARAM stConverPixelParam = new MyCamera.MV_PIXEL_CONVERT_PARAM();
            stConverPixelParam.nWidth = stFrameInfo.nWidth;
            stConverPixelParam.nHeight = stFrameInfo.nHeight;
            stConverPixelParam.pSrcData = pData;
            stConverPixelParam.nSrcDataLen = stFrameInfo.nFrameLen;
            stConverPixelParam.enSrcPixelType = stFrameInfo.enPixelType;
            stConverPixelParam.enDstPixelType = enDstPixelType;
            stConverPixelParam.pDstBuffer = pImage;
            stConverPixelParam.nDstBufferSize = m_nBufSizeForSaveImage;
            nRet = m_pMyCamera[camIndex].Cam_Info.MV_CC_ConvertPixelType_NET(ref stConverPixelParam);
            if (MyCamera.MV_OK != nRet)
            {
                return;
            }

            if (enDstPixelType == MyCamera.MvGvspPixelType.PixelType_Gvsp_Mono8)
            {
                //************************Mono8 转 Bitmap*******************************
                bmp = new Bitmap(stFrameInfo.nWidth, stFrameInfo.nHeight, stFrameInfo.nWidth * 1, PixelFormat.Format8bppIndexed, pImage);

                ColorPalette cp = bmp.Palette;
                // init palette
                for (int i = 0; i < 256; i++)
                {
                    cp.Entries[i] = Color.FromArgb(i, i, i);
                }
                // set palette back
                bmp.Palette = cp;
                //bitmap = (Bitmap)bmp.Clone();
            }
            else
            {
                //*********************RGB8 转 Bitmap**************************
                for (int i = 0; i < stFrameInfo.nHeight; i++)
                {
                    for (int j = 0; j < stFrameInfo.nWidth; j++)
                    {
                        byte chRed = m_pBufForSaveImage[i * stFrameInfo.nWidth * 3 + j * 3];
                        m_pBufForSaveImage[i * stFrameInfo.nWidth * 3 + j * 3] = m_pBufForSaveImage[i * stFrameInfo.nWidth * 3 + j * 3 + 2];
                        m_pBufForSaveImage[i * stFrameInfo.nWidth * 3 + j * 3 + 2] = chRed;
                    }
                }
                try
                {
                    bmp = new Bitmap(stFrameInfo.nWidth, stFrameInfo.nHeight, stFrameInfo.nWidth * 3, PixelFormat.Format24bppRgb, pImage);
                }
                catch
                {
                }

            }

        }

        private Boolean IsMonoData(MyCamera.MvGvspPixelType enGvspPixelType)
        {
            switch (enGvspPixelType)
            {
                case MyCamera.MvGvspPixelType.PixelType_Gvsp_Mono8:
                case MyCamera.MvGvspPixelType.PixelType_Gvsp_Mono10:
                case MyCamera.MvGvspPixelType.PixelType_Gvsp_Mono10_Packed:
                case MyCamera.MvGvspPixelType.PixelType_Gvsp_Mono12:
                case MyCamera.MvGvspPixelType.PixelType_Gvsp_Mono12_Packed:
                    return true;

                default:
                    return false;
            }
        }
        private Boolean IsColorData(MyCamera.MvGvspPixelType enGvspPixelType)
        {
            switch (enGvspPixelType)
            {
                case MyCamera.MvGvspPixelType.PixelType_Gvsp_BayerGR8:
                case MyCamera.MvGvspPixelType.PixelType_Gvsp_BayerRG8:
                case MyCamera.MvGvspPixelType.PixelType_Gvsp_BayerGB8:
                case MyCamera.MvGvspPixelType.PixelType_Gvsp_BayerBG8:
                case MyCamera.MvGvspPixelType.PixelType_Gvsp_BayerGR10:
                case MyCamera.MvGvspPixelType.PixelType_Gvsp_BayerRG10:
                case MyCamera.MvGvspPixelType.PixelType_Gvsp_BayerGB10:
                case MyCamera.MvGvspPixelType.PixelType_Gvsp_BayerBG10:
                case MyCamera.MvGvspPixelType.PixelType_Gvsp_BayerGR12:
                case MyCamera.MvGvspPixelType.PixelType_Gvsp_BayerRG12:
                case MyCamera.MvGvspPixelType.PixelType_Gvsp_BayerGB12:
                case MyCamera.MvGvspPixelType.PixelType_Gvsp_BayerBG12:
                case MyCamera.MvGvspPixelType.PixelType_Gvsp_BayerGR10_Packed:
                case MyCamera.MvGvspPixelType.PixelType_Gvsp_BayerRG10_Packed:
                case MyCamera.MvGvspPixelType.PixelType_Gvsp_BayerGB10_Packed:
                case MyCamera.MvGvspPixelType.PixelType_Gvsp_BayerBG10_Packed:
                case MyCamera.MvGvspPixelType.PixelType_Gvsp_BayerGR12_Packed:
                case MyCamera.MvGvspPixelType.PixelType_Gvsp_BayerRG12_Packed:
                case MyCamera.MvGvspPixelType.PixelType_Gvsp_BayerGB12_Packed:
                case MyCamera.MvGvspPixelType.PixelType_Gvsp_BayerBG12_Packed:
                case MyCamera.MvGvspPixelType.PixelType_Gvsp_RGB8_Packed:
                case MyCamera.MvGvspPixelType.PixelType_Gvsp_YUV422_Packed:
                case MyCamera.MvGvspPixelType.PixelType_Gvsp_YUV422_YUYV_Packed:
                case MyCamera.MvGvspPixelType.PixelType_Gvsp_YCBCR411_8_CBYYCRYY:
                    return true;

                default:
                    return false;
            }
        }
    }
}
